package com.minivision.sdktestdemo.theme.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;

/**
 * Created by Administrator on 2016/6/21.
 */
public class ThemeUtils {
    public static final String THEME = "theme";
    public static final String ACTIVITY_THEME = "activity_theme";
    public static final String THEME_TYPE = "theme_type";
    public static final String THEME_COLOR = "theme_color";
    private static int themeId = R.style.redActivityTheme;
    private static int primaryColor = Color.GREEN;
    private static Resources.Theme theme = null;
    public static final int RED_THEME = 0;
    public static final int WHITE_THEME = 1;
    public static final int DIY_THEME = 2;
    public static final int DARK_THEME = 3;
    private static int themeType = 0;
    private static String[] themeName = {"官方红", "官方白", "自选颜色", ""};

    public static String getThemeName() {
        return themeName[themeType];
    }

    public static int getThemeType() {
        return themeType;
    }

    public static void setThemeType(int type) {
        themeType = type;
        switch (type) {
            case RED_THEME:
                themeId = R.style.redActivityTheme;
                break;
            case WHITE_THEME:
                themeId = R.style.whiteActivityTheme;
                break;
            case DIY_THEME:
                themeId = R.style.redActivityTheme;
                break;
        }
    }

    public static void loadTheme(Activity context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(THEME, Context.MODE_APPEND);
        themeId = sharedPreferences.getInt(ACTIVITY_THEME, R.style.redActivityTheme);
        themeType = sharedPreferences.getInt(THEME_TYPE, themeType);
        primaryColor = sharedPreferences.getInt(THEME_COLOR, primaryColor);
        changeTheme(context);
    }

    public static void setThemeColor(int color) {
        primaryColor = color;
    }

    public static int getThemeColor() {
        return primaryColor;
    }

    public static Resources.Theme getTheme() {
        return theme;
    }

    public static int getThemeId() {
        return themeId;
    }


    public static void saveTheme(Activity context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(THEME, Context.MODE_APPEND);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(ACTIVITY_THEME, themeId);
        editor.putInt(THEME_TYPE, themeType);
        editor.putInt(THEME_COLOR, primaryColor);
        editor.commit();
    }

    public static void swithTheme(Activity context) {
        if (themeId != R.style.darkActivityTheme) {
            themeId = R.style.darkActivityTheme;
        } else {
            themeId = R.style.redActivityTheme;
        }
    }

    public static void initTheme(View view) {
        changeTheme(view, theme);
    }

    public static void changeTheme(Activity context, int layout) {
        context.setTheme(themeId);
        theme = context.getTheme();
        changeTheme(context.findViewById(layout), context.getTheme());
    }

    public static void changeTheme(Activity context) {
        changeTheme(context, R.id.drawer);
    }

    private static void changeTheme(View root, Resources.Theme theme) {
        if (root != null && root instanceof CustomThemeInterface) {
            ((CustomThemeInterface) root).setTheme(theme);
        }
        if (root != null && root instanceof ViewGroup) {
            int count = ((ViewGroup) root).getChildCount();
            for (int i = 0; i < count; ++i) {
                View view = ((ViewGroup) root).getChildAt(i);
                changeTheme(view, theme);
            }
        }
    }

    private static PopupWindow sPopWindow;

    public static void beginChange(final Activity context) {
//        WindowManager windowManager = context.getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
//        View decorView = context.getWindow().getDecorView();
//        decorView.setDrawingCacheEnabled(true);
//        final Bitmap backGround = decorView.getDrawingCache();
//        sPopWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        sPopWindow.setContentView(LayoutInflater.from(context).inflate(R.layout.layout_theme_change, null));
//        final SimpleDraweeView back = (SimpleDraweeView) sPopWindow.getContentView().findViewById(R.id.theme_back);
//        back.setImageBitmap(backGround);
//        sPopWindow.setClippingEnabled(false);
//        sPopWindow.showAtLocation(context.findViewById(R.id.drawer), Gravity.NO_GRAVITY, 0, 0);
//        sPopWindow.getContentView().animate().alpha(0.0f).setDuration(2000).setListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                sPopWindow.getContentView().setAlpha(0.0f);
//                sPopWindow.dismiss();
//                sPopWindow = null;
//            }
//            @Override
//            public void onAnimationStart(Animator animation) {
//            }
//        }).start();
        changeTheme(context);
    }

    public static boolean isDarkTheme() {
        return themeId == R.style.darkActivityTheme;
    }
}
